const { Router } = require('express')
let router = new Router()
let bodyParser = require("body-parser");
const orderModel = require('../models/orderModel');
const petModel = require('../models/petModel');
const agencyModel = require('../models/agencyModel');

//查看自己的订单
router.get('/getYourOrder', async (req, res) => {
  const { user_id } = req.query
  let order = await orderModel.find({ user_id })
  if (order) {
    res.send({ success: true, data: order, message: '查询成功' })
  } else {
    res.send({ success: false, message: '查询失败' })
  }
})

//查看订单详情


//增加订单
router.post('/createOrder', bodyParser.json(), async (req, res) => {
  const { user_id, pid, aid, name, tel, create_time, book_time, book_date } = req.body
  let oid = 'o' + Date.now()
  let pet = await orderModel.findOne({ pid, user_id })
  if (pet) {
    res.send({ success: false, message: '你已经下单了', data: pet })
  } else {
    let orderInfo = await orderModel.create({ oid, user_id, pid, aid, name, tel, create_time, book_time, book_date })
    if (orderInfo) {
      res.send({ success: true, message: '下订成功', data: orderInfo })
    } else {
      res.send({ success: false, message: '订单失败' })
    }
  }

})

//订单详情
router.get('/getOrderInfo', async (req, res) => {
  const { oid, user_id, aid } = req.query
  if (oid) {
    let orderInfo = await orderModel.findOne({ oid })
    if (orderInfo) {
      res.send({ success: true, data: orderInfo, message: '查询成功' })
    } else {
      res.send({ success: false, message: '查询失败' })
    }
  } else if (user_id) {
    let allPetInfo = await petModel.find()
    let orderInfo = await orderModel.find({ user_id })
    let agencyInfo = await agencyModel.find()
    if (orderInfo.length > 0) {
      let petInfo = JSON.parse(JSON.stringify(orderInfo))
      petInfo.forEach(oItem => {
        allPetInfo.forEach(item => {
          if (item.id === oItem.pid) {
            oItem.pname = item.pname
            oItem.profile = item.profile
            oItem.gender = item.gender
            oItem.type = item.type
            agencyInfo.forEach(aItem => {
              if (item.belong === aItem.aid) {
                oItem.local = aItem.local
                oItem.aname = aItem.real_name
                oItem.tel = aItem.tel
              }
            })
          }
        })
      })
      res.send({ success: true, data: petInfo })
    }
  } else if (aid) {
    let orderInfo = await orderModel.find({ aid })
    res.send({ success: true, data: orderInfo })
  } else {
    let orderInfo = await orderModel.find()
    res.send({ success: true, data: orderInfo })
  }

})

router.get('/orderInfo', async (req, res) => {
  try {
    const { aid, oid, pass, currentPage, pageSize } = req.query;
    let pageNumber = (currentPage - 1) * (+pageSize);
    let params = {},
      result = {};
    if (aid)
      params.aid = aid;
    if (oid)
      params.oid = oid;
    if (pass == 0 || pass == 1 || pass == 2)
      params.pass = pass;
    if (currentPage && pageSize) {
      result.data = await orderModel.find(params).skip(pageNumber).limit(+pageSize);
      result.total = await orderModel.find(params).countDocuments();
    } else {
      result = await order.find(parmas);
    }
    res.send({
      success: true,
      data: result
    })
  } catch (e) {
    res.send({
      success: false,
      data: [],
      msg: e
    })
  }
})

// 更新订单状态（审核状态）
router.post('/order/update', async (req, res) => {
  try {
    const { pass, isExist, oid } = req.body;
    let params = {};
    if (pass)
      params.pass = pass;
    if (typeof isExist === 'boolean')
      params.isExist = isExist;
    let result = await orderModel.updateOne(
      {
        oid
      },
      params
    )
    res.send({
      success: true,
      data: result
    })
  } catch (e) {
    res.send({
      success: false,
      data: [],
      msg: e
    })
  }
})


module.exports = router